<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="租约保理申请ID" prop="factoringId">
        <el-input
          v-model="queryParams.factoringId"
          placeholder="请输入租约保理申请ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户id" prop="businessId">
        <el-input
          v-model="queryParams.businessId"
          placeholder="请输入商户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出房合同ID" prop="cfContractId">
        <el-input
          v-model="queryParams.cfContractId"
          placeholder="请输入出房合同ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房产编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房产编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房产信息(考虑在房产表中增加house_name，然后带出)" prop="houseName">
        <el-input
          v-model="queryParams.houseName"
          placeholder="请输入房产信息(考虑在房产表中增加house_name，然后带出)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房源ID" prop="roomId">
        <el-input
          v-model="queryParams.roomId"
          placeholder="请输入房源ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房源编号" prop="roomCode">
        <el-input
          v-model="queryParams.roomCode"
          placeholder="请输入房源编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房源名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入房源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租客ID" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租客ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租客姓名" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入租客姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租客手机号" prop="tenantPhone">
        <el-input
          v-model="queryParams.tenantPhone"
          placeholder="请输入租客手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应收租约金额" prop="shouldMonthRent">
        <el-input
          v-model="queryParams.shouldMonthRent"
          placeholder="请输入应收租约金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保理授信额度" prop="factoringCreditMoney">
        <el-input
          v-model="queryParams.factoringCreditMoney"
          placeholder="请输入保理授信额度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保理利息" prop="factoringInterest">
        <el-input
          v-model="queryParams.factoringInterest"
          placeholder="请输入保理利息"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务费" prop="serviceFee">
        <el-input
          v-model="queryParams.serviceFee"
          placeholder="请输入服务费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借款周期(多少个月 = 付几 * 剩余未付房租账期数)" prop="factoringPeriod">
        <el-input
          v-model="queryParams.factoringPeriod"
          placeholder="请输入借款周期(多少个月 = 付几 * 剩余未付房租账期数)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借款账期开始日" prop="borrowedStart">
        <el-date-picker clearable
          v-model="queryParams.borrowedStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择借款账期开始日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="借款账期截止日" prop="borrowedEnd">
        <el-date-picker clearable
          v-model="queryParams.borrowedEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择借款账期截止日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="删除标识" prop="delFlg">
        <el-input
          v-model="queryParams.delFlg"
          placeholder="请输入删除标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建用户id" prop="createUserid">
        <el-input
          v-model="queryParams.createUserid"
          placeholder="请输入创建用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新用户id" prop="updateUserid">
        <el-input
          v-model="queryParams.updateUserid"
          placeholder="请输入更新用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:detail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['finance:detail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['finance:detail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="租约保理申请ID" align="center" prop="factoringId" />
      <el-table-column label="商户id" align="center" prop="businessId" />
      <el-table-column label="出房合同ID" align="center" prop="cfContractId" />
      <el-table-column label="房产编号" align="center" prop="houseId" />
      <el-table-column label="房产信息(考虑在房产表中增加house_name，然后带出)" align="center" prop="houseName" />
      <el-table-column label="房源ID" align="center" prop="roomId" />
      <el-table-column label="房源编号" align="center" prop="roomCode" />
      <el-table-column label="房源名称" align="center" prop="roomName" />
      <el-table-column label="租客ID" align="center" prop="tenantId" />
      <el-table-column label="租客姓名" align="center" prop="tenantName" />
      <el-table-column label="租客手机号" align="center" prop="tenantPhone" />
      <el-table-column label="应收租约金额" align="center" prop="shouldMonthRent" />
      <el-table-column label="保理授信额度" align="center" prop="factoringCreditMoney" />
      <el-table-column label="保理利息" align="center" prop="factoringInterest" />
      <el-table-column label="服务费" align="center" prop="serviceFee" />
      <el-table-column label="借款周期(多少个月 = 付几 * 剩余未付房租账期数)" align="center" prop="factoringPeriod" />
      <el-table-column label="借款账期开始日" align="center" prop="borrowedStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowedStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="借款账期截止日" align="center" prop="borrowedEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowedEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除标识" align="center" prop="delFlg" />
      <el-table-column label="创建用户id" align="center" prop="createUserid" />
      <el-table-column label="更新用户id" align="center" prop="updateUserid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租约保理明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租约保理申请ID" prop="factoringId">
          <el-input v-model="form.factoringId" placeholder="请输入租约保理申请ID" />
        </el-form-item>
        <el-form-item label="商户id" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商户id" />
        </el-form-item>
        <el-form-item label="出房合同ID" prop="cfContractId">
          <el-input v-model="form.cfContractId" placeholder="请输入出房合同ID" />
        </el-form-item>
        <el-form-item label="房产编号" prop="houseId">
          <el-input v-model="form.houseId" placeholder="请输入房产编号" />
        </el-form-item>
        <el-form-item label="房产信息(考虑在房产表中增加house_name，然后带出)" prop="houseName">
          <el-input v-model="form.houseName" placeholder="请输入房产信息(考虑在房产表中增加house_name，然后带出)" />
        </el-form-item>
        <el-form-item label="房源ID" prop="roomId">
          <el-input v-model="form.roomId" placeholder="请输入房源ID" />
        </el-form-item>
        <el-form-item label="房源编号" prop="roomCode">
          <el-input v-model="form.roomCode" placeholder="请输入房源编号" />
        </el-form-item>
        <el-form-item label="房源名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入房源名称" />
        </el-form-item>
        <el-form-item label="租客ID" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租客ID" />
        </el-form-item>
        <el-form-item label="租客姓名" prop="tenantName">
          <el-input v-model="form.tenantName" placeholder="请输入租客姓名" />
        </el-form-item>
        <el-form-item label="租客手机号" prop="tenantPhone">
          <el-input v-model="form.tenantPhone" placeholder="请输入租客手机号" />
        </el-form-item>
        <el-form-item label="应收租约金额" prop="shouldMonthRent">
          <el-input v-model="form.shouldMonthRent" placeholder="请输入应收租约金额" />
        </el-form-item>
        <el-form-item label="保理授信额度" prop="factoringCreditMoney">
          <el-input v-model="form.factoringCreditMoney" placeholder="请输入保理授信额度" />
        </el-form-item>
        <el-form-item label="保理利息" prop="factoringInterest">
          <el-input v-model="form.factoringInterest" placeholder="请输入保理利息" />
        </el-form-item>
        <el-form-item label="服务费" prop="serviceFee">
          <el-input v-model="form.serviceFee" placeholder="请输入服务费" />
        </el-form-item>
        <el-form-item label="借款周期(多少个月 = 付几 * 剩余未付房租账期数)" prop="factoringPeriod">
          <el-input v-model="form.factoringPeriod" placeholder="请输入借款周期(多少个月 = 付几 * 剩余未付房租账期数)" />
        </el-form-item>
        <el-form-item label="借款账期开始日" prop="borrowedStart">
          <el-date-picker clearable
            v-model="form.borrowedStart"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择借款账期开始日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="借款账期截止日" prop="borrowedEnd">
          <el-date-picker clearable
            v-model="form.borrowedEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择借款账期截止日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标识" prop="delFlg">
          <el-input v-model="form.delFlg" placeholder="请输入删除标识" />
        </el-form-item>
        <el-form-item label="创建用户id" prop="createUserid">
          <el-input v-model="form.createUserid" placeholder="请输入创建用户id" />
        </el-form-item>
        <el-form-item label="更新用户id" prop="updateUserid">
          <el-input v-model="form.updateUserid" placeholder="请输入更新用户id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail } from "@/api/finance/detail";

export default {
  name: "Detail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租约保理明细表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoringId: null,
        businessId: null,
        cfContractId: null,
        houseId: null,
        houseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        tenantId: null,
        tenantName: null,
        tenantPhone: null,
        shouldMonthRent: null,
        factoringCreditMoney: null,
        factoringInterest: null,
        serviceFee: null,
        factoringPeriod: null,
        borrowedStart: null,
        borrowedEnd: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        factoringId: [
          { required: true, message: "租约保理申请ID不能为空", trigger: "blur" }
        ],
        businessId: [
          { required: true, message: "商户id不能为空", trigger: "blur" }
        ],
        cfContractId: [
          { required: true, message: "出房合同ID不能为空", trigger: "blur" }
        ],
        roomId: [
          { required: true, message: "房源ID不能为空", trigger: "blur" }
        ],
        tenantId: [
          { required: true, message: "租客ID不能为空", trigger: "blur" }
        ],
        delFlg: [
          { required: true, message: "删除标识不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户id不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户id不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新用户时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询租约保理明细列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoringId: null,
        businessId: null,
        cfContractId: null,
        houseId: null,
        houseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        tenantId: null,
        tenantName: null,
        tenantPhone: null,
        shouldMonthRent: null,
        factoringCreditMoney: null,
        factoringInterest: null,
        serviceFee: null,
        factoringPeriod: null,
        borrowedStart: null,
        borrowedEnd: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租约保理明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租约保理明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除租约保理明细编号为"' + ids + '"的数据项？').then(function() {
        return delDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('finance/detail/export', {
        ...this.queryParams
      }, `detail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
